package com.zhou.starter.jpacomment.service.impl;

import cn.hutool.core.util.StrUtil;
import com.zhou.starter.jpacomment.service.AlterCommentService;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * @author lcz
 * @desc 修改表注释和字段注释
 * @mail lcz_0130@163.com
 * @date 2024/03/01
 */

public class OracleAlterCommentServiceImpl implements AlterCommentService {
    private String schema;

    @Override
    public void setSchema(String schema) {
        this.schema = schema;
    }

    private JdbcTemplate jdbcTemplate;

    @Override
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * 1schema 2表名称 3注释
     */
    String updateTableComment = "COMMENT ON TABLE %s.%s IS '%s'";

    /**
     * 1schema 2表名称 3字段名称 4注释
     */
    String updateColumnComment = "COMMENT ON COLUMN %s.%s.%s IS '%s'";

    @Override
    public void alterTableComment(String tableName, String tableComment) {
        jdbcTemplate.update(String.format(updateTableComment, schema, tableName.toUpperCase(), tableComment));
    }

    @Override
    public void alterColumnComment(String tableName, String columnName, String columnComment) {
        jdbcTemplate.update(String.format(updateColumnComment, schema, tableName.toUpperCase(), columnName.toUpperCase(), columnComment));
    }

    @Override
    public String getSchema() {
        return schema;
    }


    @Override
    public String hasTableComment(String tableName) {
        return jdbcTemplate.queryForObject("SELECT comments FROM user_tab_comments WHERE table_name = ? ", String.class, tableName);
    }

    @Override
    public String hasColumnComment(String tableName, String columnName) {
        return jdbcTemplate.queryForObject("SELECT comments FROM user_col_comments WHERE table_name = ? AND column_name = ? ", String.class, tableName, columnName);
    }
}
